package com.esri.core.internal.io.handler;

import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.portal.Portal;
import com.esri.core.portal.WebMapLayer;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* loaded from: classes.dex */
public class d {
    static final String a = "arcgis.com";
    private final Map<String, UserCredentials> b = new ConcurrentHashMap();
    private final Map<String, Future<UserCredentials>> c = new HashMap();

    public void a(String str, UserCredentials userCredentials) {
        if (userCredentials == null || com.esri.core.internal.util.a.a(str)) {
            return;
        }
        if (userCredentials instanceof b) {
            this.b.put(d(str), userCredentials);
        } else {
            this.b.put(str, userCredentials);
        }
    }

    public UserCredentials a(String str) throws InterruptedException, ExecutionException {
        UserCredentials userCredentials = null;
        if (com.esri.core.internal.util.a.a(str)) {
            return null;
        }
        if (this.b.containsKey(str)) {
            return this.b.get(str);
        }
        String d = d(str);
        if (com.esri.core.internal.util.a.a(d)) {
            return null;
        }
        if (this.b.containsKey(d)) {
            return this.b.get(d);
        }
        synchronized (this.c) {
            if (this.c.containsKey(d) && (userCredentials = c(d)) != null) {
                this.b.put(d, userCredentials);
            }
        }
        return userCredentials;
    }

    public boolean b(String str) {
        if (com.esri.core.internal.util.a.a(str)) {
            return false;
        }
        return this.b.containsKey(str) || this.b.containsKey(d(str));
    }

    public void a(final Portal portal, final WebMapLayer webMapLayer) throws Exception {
        if (webMapLayer.isFederated(portal.getUrl())) {
            String d = d(webMapLayer.getUrl());
            synchronized (this.c) {
                if (!this.c.containsKey(d)) {
                    this.c.put(d, com.esri.core.internal.tasks.e.b.submit(new Callable<UserCredentials>() { // from class: com.esri.core.internal.io.handler.d.1
                        @Override // java.util.concurrent.Callable
                        /* renamed from: a */
                        public UserCredentials call() throws Exception {
                            return d.this.a(portal, webMapLayer.getUrl());
                        }
                    }));
                }
            }
        }
    }

    UserCredentials a(Portal portal, String str) throws EsriSecurityException {
        UserCredentials userCredentials;
        String d = d(str);
        synchronized (this.b) {
            if (this.b.containsKey(d)) {
                userCredentials = this.b.get(d);
            } else if (portal.getCredentials() != null) {
                b bVar = new b(str, portal, d);
                this.b.put(d, bVar);
                userCredentials = bVar;
            } else {
                userCredentials = null;
            }
        }
        return userCredentials;
    }

    private UserCredentials c(String str) throws InterruptedException, ExecutionException {
        Future<UserCredentials> future = this.c.get(str);
        if (future != null) {
            return future.get();
        }
        return null;
    }

    public static boolean a(String str, String str2) {
        if (str == null || str2 == null) {
            return false;
        }
        try {
            URI uri = new URI(str);
            URI uri2 = new URI(str2);
            String host = uri.getHost();
            String host2 = uri2.getHost();
            if (host != null && host2 != null) {
                if (!host.equals(host2)) {
                    if (!host.toLowerCase().endsWith(a)) {
                        return false;
                    }
                    if (!host2.toLowerCase().endsWith(a)) {
                        return false;
                    }
                }
                return true;
            }
            return false;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static UserCredentials a(String str, String str2, String str3, com.esri.core.internal.tasks.oauth.e eVar) {
        return new k(str, str2, str3, eVar);
    }

    public static boolean a(UserCredentials userCredentials) {
        boolean z = userCredentials != null && (userCredentials instanceof k);
        return z ? ((k) userCredentials).g() : z;
    }

    private String d(String str) {
        Matcher matcher = Pattern.compile(".*(/.*?rest/)").matcher(str);
        String str2 = "";
        while (matcher.find()) {
            str2 = matcher.group(1);
        }
        return str.substring(0, str.lastIndexOf(str2));
    }
}
